import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm, NTag } from 'naive-ui'
import { AllocateModel } from '@/api/model/allocate-model'
import { userInfo } from '~/types/user'
import { userInfoModel } from '@/api/model/userModel'
import { Ref } from 'vue'

type Props = {
  handleEditAllocate: (row: AllocateModel, type: 'edit' | 'check') => void
  fetchDelAllocate: (id: number) => void
  typeOptions: Ref<any>
  typeLoading: Ref<boolean>
  userInfo: Ref<userInfo<userInfoModel>>
}

export const useAllocateProTable = ({
  handleEditAllocate,
  fetchDelAllocate,
  typeOptions,
  typeLoading,
  userInfo
}: Props) => {
  const showEdit = (row: any) => {
    // 申请未全部通过 且不为本部门调出
    return (
      (row.allocateOutStatus !== '2' || row.allocateInStatus !== '2') &&
      row.allocateOutBranchId !== userInfo.value.orgId.toString()
    )
  }

  const showDetail = (row: any) => {
    // 申请全部通过或者为本部门调出
    return (
      (row.allocateOutStatus === '2' && row.allocateInStatus === '2') ||
      row.allocateOutBranchId === userInfo.value.orgId.toString()
    )
  }
  // 申请未全部通过 且不为本部门调出
  const showDel = (row: any) => {
    return (
      (row.allocateOutStatus !== '2' && row.allocateInStatus !== '2') ||
      row.allocateOutBranchId !== userInfo.value.orgId.toString()
    )
  }

  const columns = computed<ProTableColumn<AllocateModel>[]>(() => {
    return [
      {
        title: '调拨编号',
        key: 'allocateCode',
        query: {
          type: 'input'
        }
      },
      {
        title: '调拨名称',
        key: 'allocateName',
        query: {
          type: 'input'
        }
      },
      {
        title: '调拨资产名称',
        key: 'assetName'
        // query: {
        //   type: 'input'
        // }
      },
      {
        type: 'dict',
        key: 'typeId',
        title: '调拨资产类型',
        dictOptions: typeOptions.value
        // query: {
        //   type: 'select',
        //   dictOptions: useDictData('STATUS').value
        // }
      },
      {
        title: '调入部门',
        key: 'allocateInBranch',
        query: {
          type: 'input'
        }
      },
      {
        title: '调出部门',
        key: 'allocateOutBranch',
        query: {
          type: 'input'
        }
      },
      {
        type: 'tag',
        title: '调入状态',
        key: 'allocateInStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        type: 'tag',
        title: '调出状态',
        key: 'allocateOutStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        title: '申请人',
        key: 'receivePerson',
        query: {
          type: 'input'
        }
      },
      {
        title: '申请日期',
        key: 'receiveDate'
      },
      {
        key: 'allocateType',
        title: '调拨类型',
        // dictOptions: useDictData('ALLOCATE_TYPE').value
        render(row) {
          return (
            <>
              {row.allocateInBranchId === userInfo.value.orgId ? (
                <NTag type="info">调入</NTag>
              ) : (
                <NTag type="warning">调出</NTag>
              )}
            </>
          )
        }
      },
      {
        title: '操作',
        key: 'operation',
        render(row) {
          return (
            <>
              {showEdit(row) ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditAllocate(row, 'edit')}
                  quaternary
                >
                  修改
                </NButton>
              ) : (
                ''
              )}

              {showDetail(row) ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditAllocate(row, 'check')}
                  quaternary
                >
                  详情
                </NButton>
              ) : (
                ''
              )}

              {showDel(row) ? (
                <NPopconfirm onPositiveClick={() => fetchDelAllocate(row.id)}>
                  {{
                    trigger: () => (
                      <NButton size="small" type="error" quaternary>
                        删除
                      </NButton>
                    ),
                    default: () => '您确定要删除？'
                  }}
                </NPopconfirm>
              ) : (
                ''
              )}
            </>
          )
        }
      }
    ]
  })

  return {
    columns
  }
}
